/*
 * Copyright (c) 2018. QDU 2015 Java1 Team CompilationError
 */

function confirmDeleteUser(userId) {
	showConfirmPopup("Confirm", "Are you sure to delete the selected designation?",
		function () {
			deleteUser(userId);
		}
	);
}

function deleteUser(userId){
	$.ajax({
		url: "delete_user",
		data: {
			// ids: [groupId]
			id: userId
		},
		type: "post",
		dataType: "json",
		// jsonp: true,
		success: function (data) {
			if(data.status=="success"){
				location.reload();
			} else {
				showMessagePopup("error", data.errorMessage);
			}
		},

		error: function(jqHXR, textStatus, error){
			console.log(error);
		}
	});
}
var userTable;
$(function () {
	userTable = $("#user_manager_table").DataTable({
		select:true
	});
	$("#filter_input").on("keyup change", function () {
		var index = $("#filter_select").val();
		userTable.column(index).search($("#filter_input").val()).draw();
	});
	$("#filter_select").on("change", function () {
		var index = $("#filter_select").val();
		$("#filter_input").val(userTable.column(index).search());
	});
});

function clearFilters(){
	$("#filter_input").val('');
	userTable.search('')
		.columns().search('')
		.draw();
}

function confirmDeleteBatch(){
	showConfirmPopup("Confirm", "Are you sure to delete the selected users?",
		deleteSelected
	);
}

function deleteSelected(){
	var ids = [];

	var rows = userTable.rows('.selected').data();

	rows.each(function (val) {
		ids.push(val[0]);
	});

	$.ajax({
		url: "delete_users",
		data: {
			ids: ids
		},
		type: "post",
		dataType: "json",
		// jsonp: true,
		success: function (data) {
			if(data.status=="success"){
				location.reload();
			} else {
				showMessagePopup("error", data.errorMessage);
			}
		},

		error: function(jqHXR, textStatus, error){
			console.log(error);
		}
	});
}